package aod.android.com.bernay.view.activity;

import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import aod.android.com.bernay.R;
import aod.android.com.bernay.base.BernayBaseActivity;
import aod.android.com.bernay.http.BernayApi;
import aod.android.com.bernay.http.BernayCallback;
import aod.android.com.bernay.live.adapter.InfoAdapter;
import aod.android.com.bernay.live.bean.HomeListBean;
import aod.android.com.bernay.live.bean.LiveHFBean;
import aod.android.com.bernay.util.SharePreUtils;
import chen.lion.hilib.image.ImageLoader;
import chen.lion.hilib.view.bind.HiView;
import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

public class PhotoInfoActivity extends BernayBaseActivity {


    private List<LiveHFBean> mDatas = new ArrayList<>();


    private InfoAdapter mAdapter;

    @HiView(R.id.scro_view)
    private ScrollView mScrollView;


    @HiView(R.id.data_list)
    private ListView mListView;

    @HiView(R.id.send)
    private EditText mSendEdit;

    @HiView(R.id.iamges_layout)
    private LinearLayout mImagesLayout;

//    @HiView(R.id.title_video)
//    private TextView mTitleTv;


    @HiView(R.id.live_user_name_tv)
    private TextView mUserNameTv;


    @HiView(R.id.content)
    private TextView mContentTv;
    @Override
    protected int initLayout() {
        return R.layout.activity_photo_info;
    }

    private SimpleDateFormat mFormat = new SimpleDateFormat("MM.dd HH:mm");

    private boolean isHF = false;
    //直播ID
    private String liveId = " ";
    private HomeListBean.DataBean dataBean =  new HomeListBean.DataBean();
    @Override
    protected void initData() {
        if(getIntent().getSerializableExtra("liveBean")!=null){
            dataBean = (HomeListBean.DataBean) getIntent().getSerializableExtra("liveBean");
        }
        initTitle(dataBean.getTitle(), R.drawable.icon_fixing_share, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showError("分享施工中");
            }
        });
        setStatusBarFontDark(false);
        initDatas();
        mAdapter = new InfoAdapter(mDatas, this);

//        mTitleTv.setText(dataBean.getTitle());
        mUserNameTv.setText(dataBean.getNickName());
        mContentTv.setText(dataBean.getDetail());
        mListView.setAdapter(mAdapter);
        mScrollView.smoothScrollTo(0, 0);

        mSendEdit.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(final TextView v, int actionId, KeyEvent event) {

                if (actionId == EditorInfo.IME_ACTION_SEND) {
                  //  showLoding();
                    BernayApi.http().postVideoText(SharePreUtils.newInstance(PhotoInfoActivity.this).getUserId(),
                            SharePreUtils.newInstance(PhotoInfoActivity.this).getUserId(),
                            dataBean.getId(), v.getText().toString(), new BernayCallback() {
                        @Override
                        public void onJsonData(String json) {
                            LiveHFBean bean1 = new LiveHFBean();
                            bean1.setUserName("测试发送账号");
                            bean1.setUserUrl("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3703406506,2565705466&fm=27&gp=0.jpg");
                            bean1.setDate(mFormat.format(new Date()));
                            bean1.setLove(10);
                            bean1.setuId(12);
                            bean1.setContent(v.getText().toString());
                            mDatas.add(bean1);
                            mAdapter.notifyDataSetChanged();
                        //    showSuccess();
                            mSendEdit.setText("");
                        }

                        @Override
                        public void onHttpFailure(IOException e) {

                        }
                    });

                }
                return false;
            }
        });

        for(String uri : dataBean.getSourceStr().split(",")){
            addImage("http://ppzlq412k.bkt.clouddn.com/"+uri);

        }
      //  addImage("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=3364854822,3695791526&fm=27&gp=0.jpg");
       // addImage("https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=3256458641,4287507862&fm=26&gp=0.jpg");
    }


    public void getData() {
        BernayApi.http().getVideoInfo(SharePreUtils.newInstance(PhotoInfoActivity.this).getUserId(),
                SharePreUtils.newInstance(PhotoInfoActivity.this).getToken(), dataBean.getId(), new BernayCallback() {
            @Override
            public void onJsonData(String json) {

            }

            @Override
            public void onHttpFailure(IOException e) {

            }
        });
    }


    public void addImage(final String path) {

        ImageView imageView = new ImageView(mContext);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 300);

        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

        // ImageLoader.with(this).load(path,imageView);
        Glide.with(this)
                .load(path)
                .into(imageView);

        imageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle b = new Bundle();
                b.putString("path",path);
                goActivity(ImageInfoActivity.class,b);
            }
        });
        mImagesLayout.addView(imageView);

    }

    public void initDatas() {
        LiveHFBean bean1 = new LiveHFBean();
        bean1.setUserName("我爱可可西里");
        bean1.setUserUrl("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2947370426,1430221852&fm=27&gp=0.jpg");
        bean1.setDate("04.10 10:45");
        bean1.setLove(10);
        bean1.setuId(12);
        bean1.setContent("取消塑料品，多用竹器，无污染！");

        LiveHFBean bean2 = new LiveHFBean();
        bean2.setUserName("混世魔王");
        bean2.setUserUrl("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2011199424,1031715062&fm=27&gp=0.jpg");
        bean2.setDate("04.10 10:47");
        bean2.setLove(40);
        bean2.setuId(13);
        bean2.setContent("有点意思！");

        LiveHFBean bean3 = new LiveHFBean();
        bean3.setUserName("李小美");
        bean3.setUserUrl("https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1479419211,3323294303&fm=27&gp=0.jpg");
        bean3.setDate("04.10 10:49");
        bean3.setLove(20);
        bean3.setuId(14);
        bean3.setContent("什么有点意思？");
        bean3.setoContent("有点意思！");
        bean3.setoId(13);
        bean3.setoName("混世魔王");


        LiveHFBean bean4 = new LiveHFBean();
        bean4.setUserName("混世魔王");
        bean4.setUserUrl("https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=2011199424,1031715062&fm=27&gp=0.jpg");
        bean4.setDate("04.10 10:59");
        bean4.setLove(20);
        bean4.setuId(14);
        bean4.setContent("你就这么想知道么？");
        bean4.setoContent("什么有点意思！");
        bean4.setoId(13);
        bean4.setoName("李小美");
//        mDatas.add(bean1);
//        mDatas.add(bean2);
//        mDatas.add(bean3);
//        mDatas.add(bean4);


    }
}
